// add/addFacility/addFacility.js
import http from '../../http'
let FormData = require('../../components/formData/formData.js')
Page({

	/**
	 * 页面的初始数据
	 */
	data: {
		lightId: '',
		name: '',
		sn: '',
		sqr: '',
		productId: '',
		parentId: '',
		labelId: '',
		remarks: '',
		status: '0',


		facilityTypeShow: false,
		facilityTypeList: [],
		facilityType: '',

		facilityParentShow: false,
		facilityParentList: [],
		facilityParent: '',

		labelShow: false,
		labelList: [{
			text: '主灯',
			value: '1'
		}, {
			text: '辅灯',
			value: '2'
		}, {
			text: '花灯',
			value: '3'
		}, {
			text: '显示屏',
			value: '4'
		}],
		label: '',
	},
		// 设备类型选择
		facilityTypeClick() {
			this.setData({
				facilityTypeShow: true
			})
		},
		facilityTypeConfirm(event) {
			this.setData({
				facilityType: event.detail.value.text,
				productId: event.detail.value.value,
				facilityTypeShow: false
			})
		},
		facilityTypeClose() {
			this.setData({
				facilityTypeShow: false
			})
		},
	// sn扫码
	snScan() {
		wx.scanCode({
			scanType: ['barCode', 'qrCode', 'datamatrix', 'pdf417'],
			success: (res) => {
				console.log('二维码数据', res)
				this.setData({
					sn: res.result
				})
			}
		})
	},
	// sqr扫码
	sqrScan() {
		wx.scanCode({
			scanType: ['barCode', 'qrCode', 'datamatrix', 'pdf417'],
			success: (res) => {
				console.log('二维码数据', res)
				this.setData({
					sqr: res.result
				})
			}
		})
	},
	// 设备父类id选择
	facilityParentClick() {
		this.setData({
			facilityParentShow: true
		})
	},
	facilityParentConfirm(event) {
		this.setData({
			facilityParent: event.detail.value.text,
			parentId: event.detail.value.value,
			facilityParentShow: false
		})
	},
	facilityParentClose() {
		this.setData({
			facilityParentShow: false
		})
	},
	// 标签选择
	labelClick() {
		this.setData({
			labelShow: true
		})
	},
	labelConfirm(event) {
		this.setData({
			label: event.detail.value.text,
			labelId: event.detail.value.value,
			labelShow: false
		})
	},
	labelClose() {
		this.setData({
			labelShow: false
		})
	},
	addFacility() {
		wx.showLoading({
			title: '加载中...'
		});
		// 普通设备添加
		if (this.data.productId != 12) {
			let form = {
				lightId: this.data.lightId,
				name: this.data.name,
				sn: this.data.sn,
				productId: this.data.productId,
				parentId: this.data.parentId,
				labelId: this.data.labelId,
				remarks: this.data.remarks,
				status: this.data.status,
			}
			for (const key in form) {
				if (form[key] != 0 && !form[key]) {
					delete form[key]
				}
			}
			http.post({
				url: '/zg/device-instance/create',
				data: form,
				then: (res) => {
					console.log('灯杆添加设备', res);
					if (res.data.code == 0) {
						wx.showToast({
							title: '添加成功',
							icon: 'success',
						})
						wx.navigateBack({
							delta: 1
						});
					}
				}
			})
		} else {
			// 小波鼠
			var formData = new FormData();
			formData.append("username", "wzn");
			formData.append("passwd", "wzn123456");
			let newData = formData.getData();
			http.post({
				url: "/zg/audio/gtoken",
				header: {
					'content-type': newData.contentType
				},
				data: newData.buffer,
				then: (res) => {
					console.log('音柱登录', res);
					let form = {
						name: this.data.name,
						sn: this.data.sn,
						deviceMetadata:this.data.sqr,
						productId: this.data.productId,
						lightId: this.data.lightId,
						labelId: this.data.labelId,
						parentId: this.data.parentId,
						remarks: this.data.remarks,
						status: this.data.status,
					}
					for (const key in form) {
						if (form[key] != 0 && !form[key]) {
							delete form[key]
						}
					}
					http.post({
						url: '/zg/audio/deviceadd',
						data: form,
						then: (res) => {
							console.log('灯杆添加设备(音柱)', res);
							if (res.data.res) {
								wx.showToast({
									title: '添加成功',
									icon: 'success',
								})
								wx.navigateBack({
									delta: 1
								});
							} else {
								wx.showToast({
									title: '服务器返回失败',
									icon: 'error',
								})
							}
						}
					})
				}
			})
		}

	},
	/**
	 * 生命周期函数--监听页面加载
	 */
	onLoad(options) {

	},

	/**
	 * 生命周期函数--监听页面初次渲染完成
	 */
	onReady() {

	},

	/**
	 * 生命周期函数--监听页面显示
	 */
	onShow() {
		let pages = getCurrentPages()
		console.log(pages);
		let currentPage = pages[pages.length - 1]
		let options = currentPage.options
		this.setData({
			lightId: options.lampId
		})
		if (options.facilitySn) {
			this.setData({
				name: options.facilitySn.slice(-4),
				sn: options.facilitySn
			})
		}

		http.get({
			url: '/zg/device-instance/byFrameworkId',
			data: {
				framework_id: 2
			},
			then: (res) => {
				console.log('父级设备', res);
				let list = res.data.data.map(item => {
					return {
						value: item.id,
						text: item.name
					}
				})
				this.setData({
					facilityParentList: list,
				})
			}
		})
		http.get({
			url: '/zg/device-product/page',
			then: (res) => {
				console.log('设备类型', res);
				let list = res.data.data.list.map(item => {
					return {
						value: item.id,
						text: item.name
					}
				})
				this.setData({
					facilityTypeList: list,
				})
			}
		})
	},

	/**
	 * 生命周期函数--监听页面隐藏
	 */
	onHide() {

	},

	/**
	 * 生命周期函数--监听页面卸载
	 */
	onUnload() {

	},

	/**
	 * 页面相关事件处理函数--监听用户下拉动作
	 */
	onPullDownRefresh() {

	},

	/**
	 * 页面上拉触底事件的处理函数
	 */
	onReachBottom() {

	},

	/**
	 * 用户点击右上角分享
	 */
	onShareAppMessage() {

	}
})